package com.yungu.swift.api.route.v1.driver;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.api.base.BaseApi;
import com.yungu.swift.assets.driver.model.dto.DriverMessageDto;
import com.yungu.swift.assets.driver.service.DriverMessageService;
import com.yungu.swift.assets.driver.service.DriverService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.api.ApiPageParam;
import com.yungu.swift.common.model.dto.CommonMessageStatusDto;
import com.yungu.swift.common.service.CommonMessageStatusService;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：
 *
 * @author Zerrion
 * @Time 2016/12/18
 */
@Controller
@RequestMapping(value = "/api/v1/driver")
@Api(value = "【司机】【消息】司机信息中心接口", tags = "司机信息中心相关操作")
public class DriMsgApi extends BaseApi {

    @Reference
    private CommonMessageStatusService commonMessageStatusService;
    @Reference
    private DriverService driverService;
    @Reference
    private DriverMessageService driverMessageService;

    @RequestMapping(value = "/token/message/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "司机获取信息中心列表", notes = "获取司机信息中心列表接口", response = ResponseData.class)
    public ResponseData<List<DriverMessageDto>> getList(
            @ApiParam(name = "token", value = "token", required = true) @RequestParam("token") String token,
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "nowPage", value = "当前页") @RequestParam(value = "nowPage", required = false) Integer nowPage,
            @ApiParam(name = "pageType", value = "前端获取类型1：首页 2：消息中心页") @RequestParam(value = "pageType", required = false) Integer pageType) {
        //查询缓存信息
        Map<String, String> tokenResult = getDriverByToken4Cache(driverService, token);
        if (tokenResult == null) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取用户信息错误");
        }
        //业务类型
        Map<String, Object> params = new HashMap<>(16);
        params.put("targetUuid", tokenResult.get("uuid"));
        params.put("businessType", new Integer(tokenResult.get("type")));
        params.put("appid", appid);
        //截止到当前时间
        params.put("sendTime", new Date());
        //司机
        params.put("targetType", CommonConstant.DRIVER);
        params.put("agentUuid", tokenResult.get("agentUuid"));
        params.put("companyUuid", tokenResult.get("companyUuid"));
        if (pageType != null && pageType == 1) {
            //首页需要其余过滤条件
            params.put("pageHome", 1);
        }
        return driverMessageService.queryDriMsgList(params, new ApiPageParam(nowPage));
    }

    @RequestMapping(value = "/token/message/homeDel", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "首页点删除", notes = "首页点删除", response = ResponseData.class)
    public ResponseData<Boolean> homeDelByUuid(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "uuid", value = "消息uuid", required = true) @RequestParam String uuid) {
        if (StringUtils.isEmpty(uuid)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "uuid不能为空");
        }
        return commonMessageStatusService.delHomeMessagesByUuid(uuid);
    }

    @RequestMapping(value = "/token/message/del", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "根据拼接的多个uuid来进行删除消息", notes = "删除消息中心消息接口", response = ResponseData.class)
    public ResponseData<Boolean> delMessagesByUuids(

            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "uuids", value = "用,拼接的uuid", required = true) @RequestParam String uuids,
            HttpServletRequest request) {

        if (StringUtils.isEmpty(uuids)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "uuids不能为空");
        }
        String userUuid = getUserUid(request);
        if (StringUtils.isEmpty(userUuid)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取用户信息错误");
        }

        return commonMessageStatusService.delMessagesByUuids(userUuid, uuids);
    }

    @RequestMapping(value = "/token/message/delAllMsg", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "清空消息", notes = "删除消息中心消息接口", response = ResponseData.class)
    public ResponseData<Boolean> delAllMsg(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            HttpServletRequest request) {

        String userUuid = getUserUid(request);
        if (StringUtils.isEmpty(userUuid)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取用户信息错误");
        }

        return commonMessageStatusService.delAllMsg(userUuid);
    }

    @RequestMapping(value = {"/token/message/update"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "更新消息状态", notes = "更新消息状态", response = ResponseData.class)
    public ResponseData<Boolean> update(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "uuid", value = "uuid", required = true) @RequestParam String uuid,
            @ApiParam(name = "status", value = "status【冗余字段】", required = true) @RequestParam Integer status) {
        if (status == CommonConstant.MSG_STATUS_HASREAD || status == CommonConstant.MSG_STATUS_HOME_READ_DELETED) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "状态不是未读,不需要更新状态");
        }
        CommonMessageStatusDto commonMessageStatusDto = new CommonMessageStatusDto();
        commonMessageStatusDto.setUuid(uuid);
        commonMessageStatusDto.setReadTime(new Date());
        commonMessageStatusDto.setStatus(CommonConstant.MSG_STATUS_HASREAD);
        return commonMessageStatusService.edit(commonMessageStatusDto);
    }
}
